package cn.com.org.controller.lecan;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.Gson;

import cn.com.bean.Employees;
import cn.com.common.EncryptUtil;
import cn.com.org.service.Common;
import cn.com.org.service.EmployeeService;

@Controller
public class EmployeeController {

	@Autowired
	HttpServletRequest request;
	@SuppressWarnings("rawtypes")
	@Autowired 
	EmployeeService employeeService;
	@Autowired 
	Common common;
	
	 @Autowired
	 private MongoTemplate mongoTemplate;
	
	@org.springframework.web.bind.annotation.InitBinder  
	public void InitBinder(WebDataBinder dataBinder) {  
	    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
	    dateFormat.setLenient(false);  
	    dataBinder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));  
	}
	
	@RequestMapping(value = "/employee", method = RequestMethod.GET)
	public String employee(HttpServletResponse response,Map<String,Object> map) throws Exception {
		map.put("employee", "ZXY");
		return "/lccrm/sysmanage/employee";
	}
	@RequestMapping(value = "/employeelist")
	public String employeelist(HttpServletResponse response,String startdate, String enddate,
			Integer currentPage, Model model) throws Exception {
		if(currentPage==null){
			currentPage=1;
		}
		model.addAttribute("employee", "ZXY");
		//Employees employees=new Employees();
		
	/*	LocalDateTime ldDateTime=LocalDateTime.now();
		ZoneId zone = ZoneId.systemDefault();
	    Instant instant = ldDateTime.atZone(zone).toInstant();
	    java.util.Date date = Date.from(instant);*/
		List<Employees> employeeslist=employeeService.getObjectList("{}", currentPage,10,true,Employees.class, "createtime");
		
		//System.out.println("dddddddddddddd"+employeeslist.size());
		model.addAttribute("employeeslist", employeeslist);
		model.addAttribute("totalPage", 4);
		model.addAttribute("currentpage", currentPage);
		Integer pageSize=10;
		Integer count=(currentPage-1)*pageSize+1;
		model.addAttribute("itemcount", count);
		return "/lccrm/sysmanage/employeelist";
	}
	@RequestMapping(value = "/employee/edit",method = RequestMethod.GET)
	public String employeeEdit(HttpServletResponse response, Model model, String id, String status){
		
		
		Employees employees=new Employees();
		if(id!=null){
	     employees=(Employees) employeeService.getById(UUID.fromString(id),Employees.class);
		}

		model.addAttribute("employee", employees);
		return "/lccrm/sysmanage/emplayeedetail";
	}
	@SuppressWarnings("unchecked")
	@ResponseBody
	@RequestMapping(value = "/employee/edit", method = RequestMethod.POST, produces = { "application/text;charset=UTF-8" })
	public String employeeEdit(HttpServletResponse response,Employees employees,String createtime){
    
		Gson gson=new Gson();
		  
		if(employees.getId()!=null){
			//Employees	gemployees=(Employees) employeeService.getById(employees.getId(),Employees.class);
			//employees.setPassword(gemployees.getPassword());
			 String jsonstring=gson.toJson(employees);
			
		employeeService.updateObjectById(employees.getId(),jsonstring,Employees.class,"id");
		}else {
			employees.setId(UUID.randomUUID());
			String pwd =EncryptUtil.EncryptMd5OrSHA(employees.getLogname(),"MD5");
			employees.setPassword(pwd);
			employeeService.addObject(employees);
		    List<Employees> dddd=employeeService.getAll(Employees.class);
		    for (Employees employees2 : dddd) {
				UUID uid= employees2.getId();
				Employees ddddd=  (Employees) employeeService.getById(uid, Employees.class);				
			}
		}
		return "OK";
		
	}
	@ResponseBody
	@RequestMapping(value = "/employee/delete", method = RequestMethod.POST, produces = { "application/text;charset=UTF-8" })
	public String employeeDel(HttpServletResponse response, Model model, String id, String status){
		if(id==null){
			return "false";
		}
	 Integer count=employeeService.deleteObjectById(UUID.fromString(id),Employees.class);
		return "OK";
	}
}
